<?php 
class PublicSurveyView extends Module
{
	function PublicSurveyView($row)
	{
		Module::Module($row);
		
		CGlobal::$website_title = AZNet::$page['title'];
		
		$this->add_form(new PublicSurveyViewForm());
	}
}

class PublicSurveyViewForm extends Form 
{
	var $public_survey = false;
	
	function PublicSurveyViewForm()
	{
		Form::Form('PublicSurveyViewForm');
		
		$survey_id	=	Url::get('id',0);
		
		if($survey_id)
		{
			$this->public_survey = DB::select('public_survey',"id=$survey_id");
		}
		
		if($this->public_survey && $this->public_survey['view'])
		{
			$ids=Url::get('ids','');
			
			if ($ids!='')
			{
				if(!isset($_SESSION['survey']) || !$_SESSION['survey'])
				{
					$ids='('.$ids.')';
					
					DB::query('UPDATE public_survey_opinion SET count = count+1 WHERE id in '.$ids);
					
					$_SESSION['survey'] = 1;
				}
			}
		}
		else
		{
			echo "<script>
					alert('Kết quả thăm dò đư luận không tồn tại!');
					window.close();
				</script>";
		}
	}
	
	function draw()
	{
		if($this->public_survey)
		{
			global $display;
			
			$display->add('question',	$this->public_survey['question']);
			
			$total	=	DB::fetch('SELECT SUM(count) AS total_count FROM `public_survey_opinion` WHERE id_survey='.$this->public_survey['id'],'total_count',0);

			$display->add('total',$total);
			
			$re = DB::query('SELECT * FROM `public_survey_opinion` WHERE `id_survey`="'.$this->public_survey['id'].'"');
			
			$opts = array();
			
			if($re)
			{
				$i = 0;
				
				while ($opt = mysql_fetch_assoc($re))
				{
					$i++;
					
					if($total>0)
					{
						$opt['width1'] 	= round($opt['count']/$total*100);
						$opt['percent'] = round($opt['count']/$total*100);
						$opt['width1'] 	= 100-(round($opt['count']/$total*100));
					}
					else
					{
						$opt['width1'] 	= 0;
						$opt['percent'] = 0;
						$opt['width1'] 	= 0;
					}
					
					$opts[$opt['id']] = $opt;
				}
			}
			
			$display->add('opts',$opts);
			
			$display->output('PublicSurveyView');
		}
	}
}
?>